package net.mihome.app.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

import net.mihome.app.AppContext;
import net.mihome.app.R;
import net.mihome.app.widget.LoadingDialog;

public class ToResetPasswordActivity extends BaseActivity {
	View cnacleButtonView;
	View okButtonView;
	EditText firstEditText;
	EditText twoEditText;
	AppContext appContext;
	Handler handler;
	String userName;
	final static int SUCCESS=1;
	final static int FAILED=2;
	final static int EXCEPTION=3;
	private LoadingDialog loading;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub 1
		super.onCreate(savedInstanceState);
		setContentView(R.layout.reset_password_info);
		userName=getIntent().getStringExtra("userName");
		firstEditText=(EditText) findViewById(R.id.password_first);
		twoEditText=(EditText) findViewById(R.id.password_two);
		cnacleButtonView=findViewById(R.id.reset_password_cancle);
		handler=new Handler(new Handler.Callback() {
			@Override
			public boolean handleMessage(Message arg0) {
				// TODO Auto-generated method stub
				if (loading != null)
					loading.dismiss();
				switch (arg0.what) {
				case SUCCESS:
					Toast.makeText(ToResetPasswordActivity.this,"修改成功，请登录", Toast.LENGTH_SHORT).show();
					ToResetPasswordActivity.this.finish();
					break;
				case FAILED:
					Toast.makeText(ToResetPasswordActivity.this,"修改失败，请重试", Toast.LENGTH_SHORT).show();
					break;
				case EXCEPTION:
					Toast.makeText(ToResetPasswordActivity.this,"发生异常，请重试", Toast.LENGTH_SHORT).show();
					break;
				default:
					break;
				}
				return false;
			}
		});
		appContext=(AppContext) getApplication();
		cnacleButtonView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				ToResetPasswordActivity.this.finish();
			}
		});
		okButtonView=findViewById(R.id.reset_password_ok);
		okButtonView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				String firstPassword=firstEditText.getText().toString();
				String twoPassword=twoEditText.getText().toString();
				if(firstPassword==null||twoPassword==null||firstPassword.trim().equals("")||twoPassword.trim().equals("")){
					Toast.makeText(ToResetPasswordActivity.this,"请填写密码", Toast.LENGTH_SHORT).show();
					return ;
				}
				if(!firstPassword.equals(twoPassword)){
					Toast.makeText(ToResetPasswordActivity.this,"两次密码不一致", Toast.LENGTH_SHORT).show();
					return ;
				}
				sendRequest(firstPassword);
				Toast.makeText(ToResetPasswordActivity.this,"正在重置..", Toast.LENGTH_SHORT).show();
			}
		});
	}
	public void sendRequest(final String passWord){
		loading = new LoadingDialog(this);
		loading.show();
		new Thread(new Runnable() {
			@Override
			public void run() {
				Message message=new Message();
				try{
					if(appContext.resetPassWord(userName, passWord)){
						message.what=SUCCESS;
					}else{
						message.what=FAILED;
					}
				}catch(Exception e){
					Log.d("sendRequest", e.getMessage());
					message.what=EXCEPTION;
				}
				handler.sendMessage(message);
				Log.i("########", "发送请求");
			}
		}).start();
	}
}
